package com.shruijin.wy.thread.cas.lock;

import com.shruijin.wy.thread.anno.SimpleThreadAnno;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleLock {

    Logger LOGGER  = LoggerFactory.getLogger(this.getClass());

    private volatile boolean locked =false;

    public boolean getLock(){
        synchronized (this){
            LOGGER.info("{} get the lock!",Thread.currentThread().getName());
            return true;
        }
    }

    public boolean getLock2() throws InterruptedException {
        while(locked){
            LOGGER.info("{} is waiting!",Thread.currentThread().getName());
            wait();
        }
        locked = true;
        LOGGER.info("{} get the lock!",Thread.currentThread().getName());
        locked = false;
        notify();

        return true;
    }

    public void setLock(boolean flag){
        locked = flag;
    }

}
